package com.afd.ozblip.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.adapter.PhotoListAdapter;
import com.afd.ozblip.adapter.ReviewListAdapter;
import com.afd.ozblip.adapter.SignListAdapter;
import com.afd.ozblip.entity.ReviewEntity;
import com.afd.ozblip.entity.SignEntity;
import com.afd.ozblip.helper.SignVH;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;
import com.afd.ozblip.util.StringUtil;
import com.afd.ozblip.view.CascadeView;
import com.afd.ozblip.view.CascadeView.OnRefreshListener;
import com.huewu.pla.lib.internal.PLA_AdapterView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

public class BusinessMessageListActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	// private CascadeView cascadeView;
	private LinearLayout commonRight;
	private PLA_AdapterView<SignListAdapter> mCascadeView = null;
	/****************** 其它变量 **************************/
	private List<SignEntity> signList;
	private SignListAdapter signListAdapter;
	private LayoutInflater inflater;
	private String businessId;
	private String businessName;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message_wall);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
		// 获取签到墙数据
		requestSignListData();
	}

	/**
	 * 寻找视图
	 */
	@SuppressWarnings("unchecked")
	public void findViews() {
		this.inflater = LayoutInflater.from(this);
		// this.signGV = (GridView) findViewById(R.id.gv_business_sign_list);
		// this.businessLeft = (LinearLayout)
		// findViewById(R.id.ll_business_sign_list_left);
		// this.businessRight = (LinearLayout)
		// findViewById(R.id.ll_business_sign_list_right);
		// this.cascadeView = (CascadeView) findViewById(R.id.scroll_view);
		this.commonRight = (LinearLayout) findViewById(R.id.ll_common_right);
		this.mCascadeView = (PLA_AdapterView<SignListAdapter>) findViewById(R.id.list);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		View.OnClickListener signListClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_more_layout_setting:
					Intent settingIntent = new Intent(
							BusinessMessageListActivity.this,
							SettingActivity.class);
					startActivity(settingIntent);
					break;
				case R.id.ll_common_right:
					if (commonUserInfo == null) {
						showLoginDialog();
					} else {
						Intent signedIntent = new Intent(
								BusinessMessageListActivity.this,
								AddSignedActivity.class);
						signedIntent.putExtra("businessId", businessId);
						signedIntent.putExtra("businessName", businessName);
						startActivity(signedIntent);
					}
					break;
				default:
					break;
				}
			}
		};
		this.commonRight.setOnClickListener(signListClick);

	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		Intent intent = getIntent();
		businessId = intent.getStringExtra("businessId");
		businessName = intent.getStringExtra("businessName");
	}

	/**
	 * 获取评论列表数据
	 */
	private void requestSignListData() {
		if (StringUtil.isNullOrEmpty(businessId)) {
			return;
		}
		AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
				new AsyncAction() {

					@Override
					public void doLoad() throws Exception {
						// 获取提交参数
						Map<String, Object> nearbyBusinessParamMap = new HashMap<String, Object>();
						nearbyBusinessParamMap.put("sellerId", businessId);
						nearbyBusinessParamMap.put("pageCnt", "1");
						nearbyBusinessParamMap.put("pageRow", "20");
						nearbyBusinessParamMap.put("sort", "1");
						nearbyBusinessParamMap.put("language", commonLanguage);
						// 发送请求
						byte[] commentItemList = HttpUtil.requestByPost(
								BusinessMessageListActivity.this,
								Constants.SELLER_SIGN_LIST_API,
								nearbyBusinessParamMap);
						if (commentItemList != null) {
							signList = new ParseJson()
									.parseBusinessSignListJson(commentItemList);
						}
					}

					@Override
					public void loadEnd() throws Exception {
						setSignListAdapter();
					}

				});
		ald.execute(0);
	}

	/**
	 * 调用菜单列表适配器
	 */
	public void setSignListAdapter() {
		if (CollectionUtil.UnNullAndZero(signList)) {
			this.signListAdapter = new SignListAdapter(
					BusinessMessageListActivity.this);
			this.signListAdapter.addItems(signList);
			this.mCascadeView.setAdapter(signListAdapter);
			// int position = 0;
			// for (int j = 0, len = signList.size(); j < len; j++) {
			// SignEntity sign = this.signList.get(j);
			// SignVH signVH = new SignVH();
			// View paramView = this.inflater.inflate(
			// R.layout.message_wall_item, null);
			// signVH.userNameTv = (TextView) paramView
			// .findViewById(R.id.tv_sign_item_userName);
			// signVH.businessScoreRb = (RatingBar) paramView
			// .findViewById(R.id.rb_sign_item_star);
			// signVH.signTimeTv = (TextView) paramView
			// .findViewById(R.id.tv_sign_item_addTime);
			// signVH.signContentTv = (TextView) paramView
			// .findViewById(R.id.tv_sign_item_content);
			// if (sign != null) {
			// // 昵称
			// String nickName = sign.getUserName();
			// if (!StringUtil.isNullOrEmpty(nickName)) {
			// signVH.userNameTv.setText(nickName);
			// }
			// // 评分
			// Float star = sign.getSellerStar();
			// if (star != null) {
			// signVH.businessScoreRb.setRating(star);
			// }
			// // 评价时间
			// String time = sign.getSginDate();
			// if (!StringUtil.isNullOrEmpty(time)) {
			// signVH.signTimeTv.setText(time);
			// }
			// // 评价内容
			// String content = sign.getSginNaiyo();
			// if (!StringUtil.isNullOrEmpty(content)) {
			// signVH.signContentTv.setText(content);
			// }
			// }
			// cascadeView.addImgToCascade(paramView, position);
			// position++;
			// if (position == 2) {
			// position = 0;
			// }
			// }
			// // 加载完毕，改变加载状态为否
			// cascadeView.updateLoadState(false);
			// cascadeView.onRefreshComplete();

		}
	}
}